﻿using System;
using System.Collections.Generic;
using ProtoBuf;

namespace SimpleInfrastructure.Exceptions
{
    /// <summary>
    /// 系统错误信息
    /// </summary>
    [Serializable]
    public class ExceptionMsg
    {
        public ExceptionMsg(Exception ex)
        {
            Exception = ex;
            DateTime = DateTime.Now;
            DotNetVersion = Environment.Version.Major + "." + Environment.Version.Minor + "." + Environment.Version.Build + "." + Environment.Version.Revision;
            OSVersion = Environment.OSVersion.ToString();
        }

        public Exception Exception { get; set; }

        /// <summary>
        /// 异常类型
        /// </summary>
        public string TypeName => Exception.GetType().Name;

        /// <summary>
        /// 消息内容
        /// </summary>
        public string Message => Exception.GetInnestException()?.Message ?? Exception.Message;

        /// <summary>
        /// 程序集名称
        /// </summary>
        public string Assembly => Exception.TargetSite != null ? Exception.TargetSite.Module.Assembly.FullName:null;

        /// <summary>
        /// 异常堆栈
        /// </summary>
        public string StackTrace => Exception.StackTrace;

        /// <summary>
        /// 异常源
        /// </summary>
        public string Source { get; set; }

        /// <summary>
        /// 客户端IP
        /// </summary>
        public string ClientIP { get; set; }

        /// <summary>
        /// 浏览器名称
        /// </summary>
        public string Browser { get; set; }

        /// <summary>
        /// 服务端.NET解释引擎版本
        /// </summary>
        public string DotNetVersion { get; private set; }

        /// <summary>
        /// 服务端操作系统类型
        /// </summary>
        public string OSVersion { get; private set; }

        /// <summary>
        /// 异常发生时间
        /// </summary>
        public DateTime DateTime { get; private set; }

        /// <summary>
        /// 异常发生方法
        /// </summary>
        public string Method => Exception.TargetSite!=null? Exception.TargetSite.Name:null;

    }
}
